export function  formatYmd(date = new Date()) {
  if (!(date instanceof Date)) {
    return date;
  };
  let y = date.getFullYear();
  let m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  let d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  return y + '-' + m + '-' + d;
}

export function formatHis(date = new Date()) {
  if (!(date instanceof Date)) {
    return date;
  };
  let hours = date.getHours();
  let minutes = date.getMinutes();
  minutes = minutes < 10 ? ('0' + minutes) : minutes;
  let seconds = date.getSeconds();
  seconds = seconds < 10 ? ('0' + seconds) : seconds;
  return hours + ':' + minutes + ':' + seconds;
}

export function format(date = new Date()) {
  if (!(date instanceof Date)) {
    return date;
  };
  let ymd = formatYmd(date);
  let his = formatHis(date);
  return ymd + ' ' + his;
}

export function distanceTime(date = new Date()) {
  if (!date) {
    return 0;
  };
  let nowDate = new Date();
  let currentTime = parseInt(nowDate.getTime());
  //game.create_time是从后台获取的时间，单位是毫秒
  let residualTime = date - currentTime;
  let hour = Math.floor((residualTime / 3600000) % 24); // 剩余小时
  let min = Math.floor((residualTime / 60000) % 60); // 剩余分钟
  let sec = Math.floor((residualTime / 1000) % 60); // 剩余秒数
  hour = hour < 10 ? '0' + hour : hour;
  min = min < 10 ? '0' + min : min;
  sec = sec < 10 ? '0' + sec : sec;
  let format = '';
  if (hour > 0) {
    format += `${hour}小时`;
  }
  if (min > 0) {
    format += `${min}分`;
  }
  if (sec > 0) {
    format += `${sec}秒`;
  }
  return format;
}
